package com.sunlg.smarthome.ui.warning;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.data.entity.FireDevice;
import com.sunlg.smarthome.data.entity.FireWarning;
import com.sunlg.smarthome.data.entity.SecurityDevice;
import com.sunlg.smarthome.data.entity.SecurityWarning;
import com.sunlg.smarthome.data.entity.Warning;
import com.sunlg.smarthome.data.remote.MySubscriber;
import com.sunlg.smarthome.data.remote.Result;
import com.sunlg.smarthome.data.remote.SHServiceWrapper;

import java.util.List;

/**
 * Created by sunlg on 2017/5/29.
 */

public class WarningAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<Warning> warningList;
    private SHServiceWrapper shServiceWrapper;

    public WarningAdapter(Context context, List<Warning> warningList) {
        this.context = context;
        this.warningList = warningList;
        shServiceWrapper = App.getInstance().getServiceWrapper();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_warning, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ItemViewHolder itemViewHolder = (ItemViewHolder)holder;
        Warning warning = warningList.get(position);

        if(warning instanceof SecurityWarning){
            shServiceWrapper.getSecurityDeviceById(warning.getDeviceId(),
                    new MySubscriber<Result<SecurityDevice>>() {
                        @Override
                        public void onSuccess(Result<SecurityDevice> data) {
                            if(data.getCode() == C.ActionStatus.SUCCESS){
                                SecurityDevice device = data.getData();
                                itemViewHolder.tvPos.setText(device.getDescription());
                                if(device.getType() == 0){
                                    itemViewHolder.tvType.setText("人体红外线传感器");
                                }else if(device.getType() == 1){
                                    itemViewHolder.tvType.setText("超声波传感器");
                                }
                            }
                        }

                        @Override
                        public void _onError(Throwable e) {
                            e.printStackTrace();
                        }
                    });
        }else if(warning instanceof FireWarning){
            shServiceWrapper.getFireDeviceById(warning.getDeviceId(),
                    new MySubscriber<Result<FireDevice>>() {
                        @Override
                        public void onSuccess(Result<FireDevice> data) {
                            if(data.getCode() == C.ActionStatus.SUCCESS){
                                FireDevice device = data.getData();
                                itemViewHolder.tvPos.setText(device.getDescription());
                                if(device.getType() == 0){
                                    itemViewHolder.tvType.setText("烟雾传感器");
                                }else if(device.getType() == 1){
                                    itemViewHolder.tvType.setText("火焰传感器");
                                }
                            }
                        }

                        @Override
                        public void _onError(Throwable e) {

                        }
                    });
        }

        itemViewHolder.tvDesc.setText(warning.getDescription());
        itemViewHolder.tvTime.setText(warning.getTime());
    }

    @Override
    public int getItemCount() {
        return warningList.size();
    }

    private static class ItemViewHolder extends RecyclerView.ViewHolder{

        TextView tvPos;
        TextView tvDesc;
        TextView tvType;
        TextView tvTime;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tvPos = (TextView)itemView.findViewById(R.id.tv_pos);
            tvDesc = (TextView)itemView.findViewById(R.id.tv_desc);
            tvType = (TextView)itemView.findViewById(R.id.tv_type);
            tvTime = (TextView)itemView.findViewById(R.id.tv_time);
        }
    }
}
